<template>
  <div class="dia-box">
    <el-dialog v-model="dialogVisible" title="选择商品" width="45%">
      <div class="dialog-content">
        <div class="goods-tag" v-if="props.tableType === 0">
          <div class="item" v-for="(item, index) in payGoodsList" :key="index">
            <div>{{ item.name }}</div>
            <div class="icon" @click="removeCheckout(item, index)">
              <el-icon>
                <Close />
              </el-icon>
            </div>
          </div>
        </div>
        <div v-if="props.tableType !== 0">
          <div class="p-tb-10">付费商品</div>
          <div class="goods-tag">
            <div class="item" v-for="(item, index) in isPayGoods" :key="index">
              <div>{{ item.name }}</div>
              <div class="icon" @click="removeCheckout(item, index)">
                <el-icon>
                  <Close />
                </el-icon>
              </div>
            </div>
          </div>
          <div class="p-tb-10">赠送商品</div>
          <div class="goods-tag">
            <div class="item" v-for="(item, index) in isGiveGoods" :key="index">
              <div>{{ item.name }}</div>
              <div class="icon" @click="removeCheckout(item, index)">
                <el-icon>
                  <Close />
                </el-icon>
              </div>
            </div>
          </div>
        </div>
        <div class="search-box">
          <div>商品分类</div>
          <div>
            <el-select v-model="selectGoodsState.categor" placeholder="请选择" size="large">
              <el-option v-for="item in goodCategorze" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </div>
          <div>商品名称</div>
          <div>
            <el-input placeholder="" v-model="selectGoodsState.goodsName"></el-input>
          </div>
          <div><el-button type="primary">搜索</el-button></div>
        </div>
        <div class="table-list">
          <div class="title">
            <div class="item" :style="title.style" v-for="(title, titIndex) in cloumn"
              :key="titIndex">
              {{ title.label }}
            </div>
          </div>
          <div class="goods-item" v-for="(item, index) in selectGoodsState.goodsList" :key="index">
            <div class="item" v-for="(item2, index2) in cloumn" :key="index2"
              :style="item2.style">
              <div v-if="item2.type == 'btn'" class="btn">
                <span v-if="props.tableType === 0" @click="selectGoods(item)" :style="goodsStatus(item.id) ? 'color: #ff4a3f;' : 'color: #3faae2;'">
                  {{ goodsStatus(item.id) ? '【取消】' : '【选择】' }}
                </span>
                <span v-else @click="selectGoods(item, index)" :style="item.isCheck ? 'color: #ff4a3f;' : 'color: #3faae2;'">
                  {{ item.isCheck ? '【取消】' : '【选择】' }}
                </span>
              </div>
              <div v-else-if="item2.type == 'inp'" class="inp">
                <el-input placeholder="" v-model="item[item2.prop]"></el-input>
              </div>
              <div v-else-if="item2.type == 'checkbox'">
                <el-checkbox v-model="item[item2.prop]" label="赠送" size="large" />
              </div>
              <div v-else>
                {{ item[item2.prop] }}
              </div>
            </div>
          </div>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="saveSelectGoods">保存</el-button>
          <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed, defineEmits, defineExpose, defineProps } from "vue";
import { Close } from '@element-plus/icons-vue'

const props = defineProps({
  tableType: {     //是否有赠送选项，0---无，1----有
    type: Number,
    default: 0
  }
})
const dialogVisible = ref(false)

const goodCategorze = ref([])

const payGoodsList = ref([])

const cloumn = computed(() => {
  return cloumnList.value.filter(f => !f.hide)
})

const selectGoodsState = reactive({
  categor: "",
  goodsName: "",
  goodsList: [
    {name: "商品1", isCheck: false, id: 1, categor: '中医', goodsNo: "84359316732", price: "500", isGive: false},
    {name: "商品2", isCheck: false, id: 2, categor: '中医2', goodsNo: "84359316732", price: "500", isGive: false},
    {name: "商品3", isCheck: false, id: 3, categor: '中医2', goodsNo: "84359316732", price: "500", isGive: false},
    {name: "商品4", isCheck: false, id: 4, categor: '中医2', goodsNo: "84359316732", price: "500", isGive: false},
    {name: "商品5", isCheck: false, id: 5, categor: '中医2', goodsNo: "84359316732", price: "500", isGive: false},
    {name: "商品6", isCheck: false, id: 6, categor: '中医2', goodsNo: "84359316732", price: "500", isGive: false},
    {name: "商品7", isCheck: false, id: 7, categor: '中医2', goodsNo: "84359316732", price: "500", isGive: false},
    {name: "商品8", isCheck: false, id: 8, categor: '中医2', goodsNo: "84359316732", price: "500", isGive: false},
    {name: "商品9", isCheck: false, id: 9, categor: '中医2', goodsNo: "84359316732", price: "500", isGive: false}
  ],
})
const cloumnList =  ref([
  {
    prop: "",
    label: "操作",
    style: "flex: 1;",
    type: 'btn',
    hide: false
  },
  {
    prop: "categor",
    label: "商品分类",
    style: "flex: 2;",
    type: 'text',
    hide: false
  },
  {
    prop: "name",
    label: "商品名称",
    style: "flex: 2;",
    type: 'text',
    hide: false
  },
  {
    prop: "goodsNo",
    label: "商品编号",
    style: "flex: 2.5;",
    type: 'text',
    hide: false
  },
  {
    prop: "price",
    label: "绑定售卖价",
    style: "flex: 1.5;",
    type: 'inp',
    hide: false
  },
  {
    hide: props.tableType === 0 ? true : false,
    prop: "isGive",
    label: "是否赠送",
    style: "flex: 1;",
    type: 'checkbox'
  },
])

// 添加商品弹窗选择、取消商品
const selectGoods = (item, index) => {
  if (props.tableType === 0) {
    const isCheckOutId = payGoodsList.value.map(m => m.id)
    const isHave = isCheckOutId.indexOf(item.id)
    if (isHave == -1) {
      payGoodsList.value.push(item)
    }else{
      payGoodsList.value.splice(isHave, 1)
    }
  }

  if (props.tableType !== 0) {
    selectGoodsState.goodsList[index].isCheck = !selectGoodsState.goodsList[index].isCheck
    if (!item.isCheck) {
      selectGoodsState.goodsList[index].isGive = false
    }
  }
}

// 添加商品弹窗移除已选商品
const removeCheckout = (item, index) => {
  if (props.tableType === 0) {
    const idArr = payGoodsList.value.map(m => m.id)
    const isHave = idArr.indexOf(item.id)
    if(isHave !== -1) {
      payGoodsList.value.splice(isHave, 1)
    }
  }

  if (props.tableType !== 0) {
    const len = selectGoodsState.goodsList.length
    for (let i = 0; i < len; i++) {
      if (selectGoodsState.goodsList[i].id == item.id) {
        selectGoodsState.goodsList[i].isCheck = false
        selectGoodsState.goodsList[i].isGive = false
        break;
      }
    }
  }
}

// 判断数据是是否是选中状态
const goodsStatus = (id) => {
  const idArr = payGoodsList.value.map(m => m.id)
  const isHand = idArr.indexOf(id)
  if(isHand !== -1) {
    return true
  }else{
    return false
  }
}

const emit = defineEmits(['comfirm'])

// 保存选择的商品
const saveSelectGoods = () => {
  let arr = []
  if (props.tableType === 0) {
    arr = JSON.parse(JSON.stringify(payGoodsList.value))
  }else{
    arr = selectGoodsState.goodsList.filter(f => f.isCheck)
  }
  emit('comfirm', arr)
  dialogVisible.value = false
}

const openDialog = (arr) => {
  if (arr) {
    if (props.tableType === 0) {
      payGoodsList.value = JSON.parse(JSON.stringify(arr))
    }else{
      selectGoodsState.goodsList.forEach(item => {
        item.isCheck = false
        item.isGive = false
        arr.forEach(item2 => {
          if (item.id == item2.id) {
            item.isCheck = true
            if (item2.isGive) {
              item.isGive = true
            }
          }
        })
      })
    }
  }
  dialogVisible.value = true
}

const isPayGoods = computed(() => {
  return selectGoodsState.goodsList.filter(f => !f.isGive && f.isCheck)
})

const isGiveGoods = computed(() => {
  return selectGoodsState.goodsList.filter(f => f.isGive && f.isCheck)
})

defineExpose({openDialog})
</script>

<style lang="scss" scoped>
.dia-box {
  ::v-deep .el-dialog__body {
    border-top: 1px solid #cfcfcf;
    padding: 10px 20px;
    max-height: 450px;
    overflow-y: auto;
  }
  ::v-deep .el-dialog__footer {
    background: #f0f4f7;
    text-align: center;
    padding-top: 15px;
  }
  .dialog-content {
    .goods-tag {
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 10px;
      .item {
        display: flex;
        align-items: center;
        height: 28px;
        line-height: 26px;
        border: 1px solid #1a76d3;
        padding-left: 5px;
        color: #3681d6;
        margin: 5px 5px 5px 0;
        .icon {
          cursor: pointer;
          margin-left: 3px;
          width: 25px;
          height: 26px;
          font-size: 15px;
          text-align: center;
          line-height: 30px;
          &:hover {
            background-color: #ff4a3f;
            color: #fff;
          }
        }
      }
    }
    .search-box {
      display: flex;
      align-items: center;
      padding-bottom: 10px;
      border-bottom: 1px solid #cfcfcf;
      &>div {
        margin-right: 15px;
        ::v-deep .el-input__inner {
          height: 38px;
          line-height: 38px;
        }
      }
    }
    .table-list {
      margin-top: 10px;
      border-top: 1px solid #cecece;
      border-left: 1px solid #cecece;
      .title, .goods-item {
        display: flex;
        background-color: #e7e7e7;
        color: #192942;
        font-size: 14px;
        .item {
          text-align: center;
          border-right: 1px solid #cecece;
          border-bottom: 1px solid #cecece;
          height: 40px;
          line-height: 40px;
          .btn {
            span {
              cursor: pointer;
            }
          }
          .inp {
            padding: 0 5px;
            ::v-deep .el-input__inner {
              height: 22px;
              line-height: 22px;
              text-align: center;
              font-size: 12px;
            }
          }
        }
      }
      .goods-item {
        background-color: #fff;
      }
    }
  }
}
</style>